package aplicacao.desktop.telefone;
import java.telephony.*;
import java.telephony.events.*;
import java.telephony.media.*;
import java.telephony.media.events.*;


class DesktopCallObserver implements MediaCallObserver {

  /*
   * This CallObserver will be notified of Call events.
   */
  public void callChangedEvent(CallEv evlist[]) {

    for (int i = 0; i < evlist.length; i++) {
      CallEv ev = evlist[i];

      if (ev instanceof MediaTermConnAvailableEv) {
        TerminalConnection tc = (TermConnEv)ev.getTerminalConnection();
        MediaTerminalConnection mtc = (MediaTerminalConnection)tc;

        /* Route the media to/from the workstation's hardware. */
        try {
          mtc.useDefaultSpeaker();
          mtc.useDefaultMicrophone();

          mtc.startPlaying();
          mtc.startRecording();
        } catch (Exception excp) {
          // Handle all Exceptions
        }
      }
      else if (ev instance MediaTermConnUnavailableEv) {

        /* Stop the playing and recording */
        TerminalConnection tc = (TermConnEv)ev.getTerminalConnection();
        MediaTerminalConnection mtc = (MediaTerminalConnection)tc;

        try {
          mtc.stopRecording();
          mtc.stopPlaying();
        } catch (Exception excp) {
          // Handle all Exceptions
        }
      }
    }
  }
}


